/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.ecard.web.front;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.OutputStream;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.boxin.ims.modules.ecard.entity.Ecard;
import com.boxin.ims.modules.ecard.entity.OrganEvent;
import com.boxin.ims.modules.ecard.entity.OrganInfo;
import com.boxin.ims.modules.ecard.service.EcardService;
import com.boxin.ims.modules.ecard.service.OrganEventService;
import com.boxin.ims.modules.ecard.service.OrganInfoService;
import com.boxin.ims.modules.momarketing.common.ProjectUtils;
import com.boxin.ims.modules.momarketing.entity.QRCode;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

/**
 * 公司活动Controller
 * @author Jakemanse
 * @version 2013-06-10
 */
@Controller
@RequestMapping(value= Global.MOBILE_ECARD_PATH )
public class EcardFrontController extends BaseController {

	@Autowired
	private EcardService ecardService;
	@Autowired
	private OrganEventService organEventService;
	@Autowired
	private OrganInfoService  organInfoService;
	
	
	@RequestMapping(value = {"/{id}"+ Global.URL_SUFFIX, ""})
	public String viewEcard(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response,Model model) {
		Ecard ecard = 	ecardService.get(id);
        //Page<Ecard> page = ecardService.find(new Page<Ecard>(request, response), ecard); 
		
		String path = request.getContextPath();
        String basePath = request.getScheme() + "://" + request.getLocalAddr() + ":"
                                   + request.getServerPort() + path ;
        
        String url = basePath  + Global.MOBILE_ECARD_PATH +"/view-"+ecard.getId()+Global.URL_SUFFIX;
        request.setAttribute("url", url);
        String theme = (ecard.getPageTemplate()==null || StringUtils.isEmpty(ecard.getPageTemplate().getTheme())) ?"ecard":ecard.getPageTemplate().getTheme();
		
        model.addAttribute("theme", theme);
        model.addAttribute("ecard", ecard);
        
        
		return "modules/ecard/front/"+theme;
	}
	
	/**
	 * @author Jakemanse
	 * @time 2013-7-1  下午3:32:28
	 * @function <p>  </p>
	 * @param id
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/{id}.jpg")
	public void viewEcardLogo(@PathVariable Long id,HttpServletRequest req,HttpServletResponse response) throws Exception{
		
		Ecard ecard = ecardService.get(id);
		
		String filepath = ecard.getLogo();
		File file = new File(filepath);
		responseImage(file,response);
		
	}
	
	/**
	 * @author Jakemanse
	 * @time 2013-11-8  下午1:33:06
	 * @function <p> 查看公司单位Logo </p>
	 * @param id
	 * @param req
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/organ{id}.logo")
	public void viewOrganLogo(@PathVariable Long id,HttpServletRequest req,HttpServletResponse response) throws Exception{
		
		OrganInfo organInfo =	organInfoService.get(id);
		
		String filepath = organInfo.getLogo();
		File file = new File(filepath);
		responseImage(file,response);
		
	}
	
	
	
	
	/**
	 * @author Jakemanse
	 * @time 2013-11-8  下午1:28:05
	 * @function <p> 查看文件 </p>
	 * @param file
	 * @param response
	 * @throws Exception
	 */
	public void responseImage(File file,HttpServletResponse response)throws Exception{
		
		BufferedImage image =  	ImageIO.read(file);
		OutputStream out = response.getOutputStream();
		String fileFormat = "jpg";
		String fileName = file.getName();
		if(fileName.lastIndexOf(".") != -1){
			fileFormat = fileName.substring(fileName.lastIndexOf(".")+1);
		}
		
		response.setContentType("image/"+fileFormat);
		ImageIO.write(image, fileFormat.toUpperCase(), out);
		out.flush();
		out.close();
	}
	
	
	@RequestMapping(value = {"/event-{id}"+ Global.URL_SUFFIX, ""})
	public String viewEcardEvent(@PathVariable Long id,HttpServletRequest req,HttpServletResponse response,Model model) throws Exception{
		
		OrganEvent organEvent =	organEventService.get(id);
		model.addAttribute("organEvent", organEvent);
		
		return "modules/ecard/front/viewEvent";
	}


}
